{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第一节 相关分析\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "      DJIA  SPX\nDate           \n1     7715  942\n2     7442  915\n3     7581  928\n4     7572  928\n5     7881  963\n6     7823  955\n7     8149  984\n8     7838  953\n9     7756  947\n10    7679  936",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>DJIA</th>\n      <th>SPX</th>\n    </tr>\n    <tr>\n      <th>Date</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1</th>\n      <td>7715</td>\n      <td>942</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>7442</td>\n      <td>915</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>7581</td>\n      <td>928</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>7572</td>\n      <td>928</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>7881</td>\n      <td>963</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>7823</td>\n      <td>955</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>8149</td>\n      <td>984</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>7838</td>\n      <td>953</td>\n    </tr>\n    <tr>\n      <th>9</th>\n      <td>7756</td>\n      <td>947</td>\n    </tr>\n    <tr>\n      <th>10</th>\n      <td>7679</td>\n      <td>936</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index_df = pd.read_excel('6-1.xlsx', index_col=0) # 注意索引\n",
    "index_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "              DJIA          SPX\nDJIA  38937.377778  3917.155556\nSPX    3917.155556   397.877778",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>DJIA</th>\n      <th>SPX</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>DJIA</th>\n      <td>38937.377778</td>\n      <td>3917.155556</td>\n    </tr>\n    <tr>\n      <th>SPX</th>\n      <td>3917.155556</td>\n      <td>397.877778</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 无偏估计\n",
    "index_df.cov()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "array([[35043.64,  3525.44],\n       [ 3525.44,   358.09]])"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 有偏估计\n",
    "np.cov(index_df.values , rowvar=False , ddof=0)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "利用实验6-1的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "          DJIA       SPX\nDJIA  1.000000  0.995205\nSPX   0.995205  1.000000",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>DJIA</th>\n      <th>SPX</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>DJIA</th>\n      <td>1.000000</td>\n      <td>0.995205</td>\n    </tr>\n    <tr>\n      <th>SPX</th>\n      <td>0.995205</td>\n      <td>1.000000</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法1，利用pandas中的corr方法\n",
    "index_df.corr()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "array([[1.        , 0.99520518],\n       [0.99520518, 1.        ]])"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法2，利numpy中的corrcoef方法\n",
    "np.corrcoef(index_df.values , rowvar=False)\n",
    "\n",
    "#%"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "plt.rcParams['font.sans-serif'] = ['SimHei'] # 步骤一（替换sans-serif字体）\n",
    "plt.rcParams['axes.unicode_minus'] = False   # 步骤二（解决坐标轴负数的负号显示问题）\n",
    "plt.rcParams['savefig.dpi'] = 300 # 图片质量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "<AxesSubplot:xlabel='DJIA', ylabel='SPX'>"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": "<Figure size 432x288 with 1 Axes>",
      "image/png": "iVBORw0KGgoAA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ony4PG6YdfnOEhNwF3ZOOc+vtp4+m8pfmy/KXZ/SbwZ8AO4JeA7RHxdeCpzDw+w2M2A08Uywdw3l9dyNM7SFIFueUvSRVk+UtSBVn+klRBlr8kVZDlL0kVZPlLUgX9P4pTCBngcrNPAAAAAElFTkSuQmCC\n"
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "index_df.plot.scatter(x='DJIA',y='SPX')\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "根据实验6-1的数据，建立用DJIA预测SPX的回归模型，并预测DJIA=8300时，SPX=？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.linear_model import LinearRegression\n",
    "import statsmodels.api as sm\n",
    "import statsmodels.formula.api as smf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "      const  DJIA\nDate             \n1       1.0  7715\n2       1.0  7442\n3       1.0  7581\n4       1.0  7572\n5       1.0  7881\n6       1.0  7823\n7       1.0  8149\n8       1.0  7838\n9       1.0  7756\n10      1.0  7679",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>const</th>\n      <th>DJIA</th>\n    </tr>\n    <tr>\n      <th>Date</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1</th>\n      <td>1.0</td>\n      <td>7715</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1.0</td>\n      <td>7442</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1.0</td>\n      <td>7581</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>1.0</td>\n      <td>7572</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>1.0</td>\n      <td>7881</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>1.0</td>\n      <td>7823</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>1.0</td>\n      <td>8149</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>1.0</td>\n      <td>7838</td>\n    </tr>\n    <tr>\n      <th>9</th>\n      <td>1.0</td>\n      <td>7756</td>\n    </tr>\n    <tr>\n      <th>10</th>\n      <td>1.0</td>\n      <td>7679</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = index_df[['DJIA']]\n",
    "y = index_df[['SPX']]\n",
    "X = sm.add_constant(x) # 模型包含截距项，因而需要因变量矩阵增加值为1的常数列\n",
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "const    166.082832\nDJIA       0.100601\ndtype: float64"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sm_model = sm.OLS(y, X)\n",
    "sm_result = sm_model.fit()\n",
    "sm_result.params"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "回归直线方程为 Y = 166.082832 + 0.100601X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "0.9904333423452638"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算判定系数\n",
    "sm_result.rsquared"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "判定系数很大，说明模型拟合效果较好"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "array([1001.07463095])"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 进行一元线性预测\n",
    "sm_result.predict([1,8300])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "y预测当DJIA=8300时，SPX=1001.07463095"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "Intercept    166.082832\nDJIA           0.100601\ndtype: float64"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 第二种方法，用statsmodels\n",
    "sm_model = smf.ols(formula='SPX~DJIA' , data=index_df)\n",
    "sm_result = sm_model.fit()\n",
    "sm_result.params"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "0.9904333423452638"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sm_result.rsquared"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "0    1001.074631\ndtype: float64"
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sm_result.predict(pd.DataFrame([{'DJIA':8300}]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "166.08283214871528"
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 第三种方法，用sklearn\n",
    "sk_model = LinearRegression()\n",
    "sk_model.fit(x,y)\n",
    "sk_model.intercept_[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "0.10060142154182611"
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sk_model.coef_[0][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "0.9904333423452637"
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 判定系数\n",
    "sk_model.score(x,y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "array([[1001.07463095]])"
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 预测\n",
    "sk_model.predict([[8300]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 实验6-5 多元线性回归分析和预测"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q是因变量，其他是自变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "          Q     P      M    PAI  PBMac\nMonth                                 \n1      1773  8.65  25500  10.55   1.25\n2      1863  8.65  25600  10.45   1.35\n3      1798  8.65  25700  10.35   1.55\n4      1775  8.65  25970  10.30   1.05\n5      1796  8.65  25970  10.30   0.95",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Q</th>\n      <th>P</th>\n      <th>M</th>\n      <th>PAI</th>\n      <th>PBMac</th>\n    </tr>\n    <tr>\n      <th>Month</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1</th>\n      <td>1773</td>\n      <td>8.65</td>\n      <td>25500</td>\n      <td>10.55</td>\n      <td>1.25</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1863</td>\n      <td>8.65</td>\n      <td>25600</td>\n      <td>10.45</td>\n      <td>1.35</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1798</td>\n      <td>8.65</td>\n      <td>25700</td>\n      <td>10.35</td>\n      <td>1.55</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>1775</td>\n      <td>8.65</td>\n      <td>25970</td>\n      <td>10.30</td>\n      <td>1.05</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>1796</td>\n      <td>8.65</td>\n      <td>25970</td>\n      <td>10.30</td>\n      <td>0.95</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pi_df = pd.read_excel('6-5.xlsx', index_col=0)\n",
    "pi_df "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "Intercept     976.591186\nP            8447.513762\nM              -1.709659\nPAI         -2652.159091\nPBMac        -545.000000\ndtype: float64"
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pi_model = smf.ols(formula='Q ~ P + M + PAI + PBMac' , data=pi_df)\n",
    "pi_result = pi_model.fit()\n",
    "pi_result.params"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\XinZhi\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\statsmodels\\stats\\stattools.py:74: ValueWarning: omni_normtest is not valid with less than 8 observations; 5 samples were given.\n",
      "  warn(\"omni_normtest is not valid with less than 8 observations; %i \"\n"
     ]
    },
    {
     "data": {
      "text/plain": "<class 'statsmodels.iolib.summary.Summary'>\n\"\"\"\n                            OLS Regression Results                            \n==============================================================================\nDep. Variable:                      Q   R-squared:                       0.580\nModel:                            OLS   Adj. R-squared:                 -0.682\nMethod:                 Least Squares   F-statistic:                    0.4594\nDate:                Wed, 03 Nov 2021   Prob (F-statistic):              0.763\nTime:                        10:34:37   Log-Likelihood:                -22.362\nNo. Observations:                   5   AIC:                             52.72\nDf Residuals:                       1   BIC:                             51.16\nDf Model:                           3                                         \nCovariance Type:            nonrobust                                         \n==============================================================================\n                 coef    std err          t      P>|t|      [0.025      0.975]\n------------------------------------------------------------------------------\nIntercept    976.5912    911.512      1.071      0.478   -1.06e+04    1.26e+04\nP           8447.5138   7884.577      1.071      0.478   -9.17e+04    1.09e+05\nM             -1.7097      1.632     -1.047      0.485     -22.453      19.033\nPAI        -2652.1591   2545.723     -1.042      0.487    -3.5e+04    2.97e+04\nPBMac       -545.0000    580.237     -0.939      0.520   -7917.610    6827.610\n==============================================================================\nOmnibus:                          nan   Durbin-Watson:                   3.250\nProb(Omnibus):                    nan   Jarque-Bera (JB):                0.723\nSkew:                           0.593   Prob(JB):                        0.696\nKurtosis:                       1.563   Cond. No.                     1.51e+21\n==============================================================================\n\nNotes:\n[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n[2] The smallest eigenvalue is 1.46e-33. This might indicate that there are\nstrong multicollinearity problems or that the design matrix is singular.\n\"\"\"",
      "text/html": "<table class=\"simpletable\">\n<caption>OLS Regression Results</caption>\n<tr>\n  <th>Dep. Variable:</th>            <td>Q</td>        <th>  R-squared:         </th> <td>   0.580</td>\n</tr>\n<tr>\n  <th>Model:</th>                   <td>OLS</td>       <th>  Adj. R-squared:    </th> <td>  -0.682</td>\n</tr>\n<tr>\n  <th>Method:</th>             <td>Least Squares</td>  <th>  F-statistic:       </th> <td>  0.4594</td>\n</tr>\n<tr>\n  <th>Date:</th>             <td>Wed, 03 Nov 2021</td> <th>  Prob (F-statistic):</th>  <td> 0.763</td> \n</tr>\n<tr>\n  <th>Time:</th>                 <td>10:34:37</td>     <th>  Log-Likelihood:    </th> <td> -22.362</td>\n</tr>\n<tr>\n  <th>No. Observations:</th>      <td>     5</td>      <th>  AIC:               </th> <td>   52.72</td>\n</tr>\n<tr>\n  <th>Df Residuals:</th>          <td>     1</td>      <th>  BIC:               </th> <td>   51.16</td>\n</tr>\n<tr>\n  <th>Df Model:</th>              <td>     3</td>      <th>                     </th>     <td> </td>   \n</tr>\n<tr>\n  <th>Covariance Type:</th>      <td>nonrobust</td>    <th>                     </th>     <td> </td>   \n</tr>\n</table>\n<table class=\"simpletable\">\n<tr>\n      <td></td>         <th>coef</th>     <th>std err</th>      <th>t</th>      <th>P>|t|</th>  <th>[0.025</th>    <th>0.975]</th>  \n</tr>\n<tr>\n  <th>Intercept</th> <td>  976.5912</td> <td>  911.512</td> <td>    1.071</td> <td> 0.478</td> <td>-1.06e+04</td> <td> 1.26e+04</td>\n</tr>\n<tr>\n  <th>P</th>         <td> 8447.5138</td> <td> 7884.577</td> <td>    1.071</td> <td> 0.478</td> <td>-9.17e+04</td> <td> 1.09e+05</td>\n</tr>\n<tr>\n  <th>M</th>         <td>   -1.7097</td> <td>    1.632</td> <td>   -1.047</td> <td> 0.485</td> <td>  -22.453</td> <td>   19.033</td>\n</tr>\n<tr>\n  <th>PAI</th>       <td>-2652.1591</td> <td> 2545.723</td> <td>   -1.042</td> <td> 0.487</td> <td> -3.5e+04</td> <td> 2.97e+04</td>\n</tr>\n<tr>\n  <th>PBMac</th>     <td> -545.0000</td> <td>  580.237</td> <td>   -0.939</td> <td> 0.520</td> <td>-7917.610</td> <td> 6827.610</td>\n</tr>\n</table>\n<table class=\"simpletable\">\n<tr>\n  <th>Omnibus:</th>       <td>   nan</td> <th>  Durbin-Watson:     </th> <td>   3.250</td>\n</tr>\n<tr>\n  <th>Prob(Omnibus):</th> <td>   nan</td> <th>  Jarque-Bera (JB):  </th> <td>   0.723</td>\n</tr>\n<tr>\n  <th>Skew:</th>          <td> 0.593</td> <th>  Prob(JB):          </th> <td>   0.696</td>\n</tr>\n<tr>\n  <th>Kurtosis:</th>      <td> 1.563</td> <th>  Cond. No.          </th> <td>1.51e+21</td>\n</tr>\n</table><br/><br/>Notes:<br/>[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.<br/>[2] The smallest eigenvalue is 1.46e-33. This might indicate that there are<br/>strong multicollinearity problems or that the design matrix is singular."
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pi_result.summary()"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "注意，看第二个表格中的 coef 与 t 列，\n",
    "回归方程为 Q = 976.5912 + 8447.5138P -1.7097M -2652.1591PAI -545.0000PBMac\n",
    "           (1.071)   (1.071)    (-1.047)   (-1.042）  (-0.939)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "              Q   P         M       PAI     PBMac\nQ      1.000000 NaN -0.285999  0.135738  0.343973\nP           NaN NaN       NaN       NaN       NaN\nM     -0.285999 NaN  1.000000 -0.930529 -0.683977\nPAI    0.135738 NaN -0.930529  1.000000  0.376746\nPBMac  0.343973 NaN -0.683977  0.376746  1.000000",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Q</th>\n      <th>P</th>\n      <th>M</th>\n      <th>PAI</th>\n      <th>PBMac</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Q</th>\n      <td>1.000000</td>\n      <td>NaN</td>\n      <td>-0.285999</td>\n      <td>0.135738</td>\n      <td>0.343973</td>\n    </tr>\n    <tr>\n      <th>P</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>M</th>\n      <td>-0.285999</td>\n      <td>NaN</td>\n      <td>1.000000</td>\n      <td>-0.930529</td>\n      <td>-0.683977</td>\n    </tr>\n    <tr>\n      <th>PAI</th>\n      <td>0.135738</td>\n      <td>NaN</td>\n      <td>-0.930529</td>\n      <td>1.000000</td>\n      <td>0.376746</td>\n    </tr>\n    <tr>\n      <th>PBMac</th>\n      <td>0.343973</td>\n      <td>NaN</td>\n      <td>-0.683977</td>\n      <td>0.376746</td>\n      <td>1.000000</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 单相关系数\n",
    "pi_df.corr()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "Intercept     976.591186\nP            8447.513762\nM              -1.709659\nPAI         -2652.159091\nPBMac        -545.000000\ndtype: float64"
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 偏相关系数\n",
    "pi_model_m = smf.ols(formula='Q ~ P + M + PAI + PBMac' , data=pi_df)\n",
    "pi_result_m = pi_model_m.fit()\n",
    "pi_result_m.params"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\XinZhi\\AppData\\Local\\Temp/ipykernel_78304/1957524464.py:2: RuntimeWarning: invalid value encountered in sqrt\n",
      "  np.sqrt(pi_result.params[2] * pi_result_m.params[1])\n"
     ]
    },
    {
     "data": {
      "text/plain": "nan"
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Q与M的偏相关系数为\n",
    "np.sqrt(pi_result.params[2] * pi_result_m.params[1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这一部分由于数据不全以及未提及虚拟变量，多重共线性等较深入内容，这一部分暂写至此，日后进行补充整理"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 实验6-6 非线性回归分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 建立指数回归模型 Y = β0 β1 ^ x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "        Q  Month         Q1\n0   33100      1  10.407289\n1   47300      2  10.764266\n2   69000      3  11.141862\n3  102000      4  11.532728\n4  150000      5  11.918391\n5  220000      6  12.301383",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Q</th>\n      <th>Month</th>\n      <th>Q1</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>33100</td>\n      <td>1</td>\n      <td>10.407289</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>47300</td>\n      <td>2</td>\n      <td>10.764266</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>69000</td>\n      <td>3</td>\n      <td>11.141862</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>102000</td>\n      <td>4</td>\n      <td>11.532728</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>150000</td>\n      <td>5</td>\n      <td>11.918391</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>220000</td>\n      <td>6</td>\n      <td>12.301383</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ma_df = pd.read_excel('6-6.xlsx')\n",
    "ma_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "        Q  Month         Q1\n0   33100      1  10.407289\n1   47300      2  10.764266\n2   69000      3  11.141862\n3  102000      4  11.532728\n4  150000      5  11.918391\n5  220000      6  12.301383",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Q</th>\n      <th>Month</th>\n      <th>Q1</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>33100</td>\n      <td>1</td>\n      <td>10.407289</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>47300</td>\n      <td>2</td>\n      <td>10.764266</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>69000</td>\n      <td>3</td>\n      <td>11.141862</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>102000</td>\n      <td>4</td>\n      <td>11.532728</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>150000</td>\n      <td>5</td>\n      <td>11.918391</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>220000</td>\n      <td>6</td>\n      <td>12.301383</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 先进性线性化得到线性模型： lnY = lnβ0 + lnβ1 X\n",
    "ma_df.loc[:,'Q1'] = np.log(ma_df['Q'])\n",
    "ma_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "Intercept    10.011948\nMonth         0.380678\ndtype: float64"
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ma_model = smf.ols(formula='Q1 ~ Month' , data=ma_df)\n",
    "ma_result = ma_model.fit()\n",
    "ma_result.params"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "22291.22329846546"
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.power(np.e , ma_result.params[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "1.4632756281161743"
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.power(np.e , ma_result.params[1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "lnβ0 估计值为 10.011948\n",
    "\n",
    "lnβ1 估计值为 0.380678\n",
    "\n",
    "β0 估计值为 22291.22329846538\n",
    "\n",
    "β1 估计值为 1.4632756281161763\n",
    "\n",
    "模型为 Y = 22291.22329846538 × 1.4632756281161763^x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 总结"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这一章篇幅较短，还有很多详细的深入的内容可以介绍，日后慢慢完善。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "pycharm-999df490",
   "language": "python",
   "display_name": "PyCharm (Statistics)"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}